<body>
    <!-- 列表头高级查询 -->
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>用户查询</legend>
    </fieldset>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户编号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="userId" id="userId" autocomplete="off" class="layui-input"
                        placeholder="请输入">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户姓名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="userName" id="userName" autocomplete="off" class="layui-input"
                        placeholder="请输入">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="doSearch" lay-filter="demo1">查询</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="doReset">重置</button>
            </div>
        </div>
    </form>


    <div class="layui-btn-group demoTable">
        <button class="layui-btn" data-type="refresh" id="refreshUserBtn">刷新</button>
    </div>



    <table class="layui-hide" id="userTableId" lay-filter="userTable"></table>
    <!-- 按钮 -->
    <div style="display: none" id="userbarDemo" type="text/html">
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    </div>



    <script>
        layui.use(['jquery', 'layer', 'table', 'form', 'config', 'element'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var form = layui.form;
            var config = layui.config;
            layui.element.init();
            form.render();
            // 表格渲染
            table.render({
                elem: '#userTableId',
                id: 'user'
                , url: config.base_server + 'getUserList'
                , cellMinWidth: 80
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'userId', title: '用户编号', sort: true, minWidth: 100 }
                    , { field: 'username', title: '用户名称', minWidth: 100 }
                    , { field: 'email', title: '用户邮箱', minWidth: 100 }
                    , { title: '操作', toolbar: '#userbarDemo' }
                ]]
                , page: true //开启分页
                , skin: 'row' //行边框风格
                , even: true //开启隔行背景
                , size: 'lg' //小尺寸的表格
            });

            //行内按钮事件的监听
            table.on('tool(userTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    var editId = data.userId
                    var fu = layer.open({
                        type: 2,
                        title: "查看" + editId + "号用户",
                        area: ['600px', '600px'],
                        fix: false,
                        content: 'components/system/userDetailTable.html?editNo=' + editId,

                        maxmin: true,
                        end: function () {
                            table.reload('user', {
                                url: config.base_server + 'getUserList' //数据接口
                                , where: {} //设定异步数据接口的额外参数
                            });
                        }
                    });

                } else if (obj.event === 'del') {
                    var editId = data.userId;
                    layer.confirm('真的删除该用户吗?', function (index) {
                        $.ajax({
                            type: "GET",
                            url: config.base_server + "DeleteUserById",
                            data: { userId: editId },
                            dataType: "json",
                            success: (data) => {
                                if (data.code) {
                                    obj.del();
                                    // table.reload('user');
                                    layer.msg('删除成功', {
                                        icon: 1
                                    });
                                    layer.close(index);
                                } else {
                                    alert(data.info);
                                }
                            }
                        });
                    });
                }
            });
            $('#refreshUserBtn').on('click', function () {
                location.reload();
            });
            //  查询按钮
            $('#doSearch').on('click', function () {
                var userId = $("input[name='userId']").val();
                var userName = $("input[name='userName']").val();
                table.reload('user', {
                    url: config.base_server + 'getUserList' //数据接口
                    , where: { userId: userId, userName: userName }
                });

            });
            //重置：待解决刷新问题 
            $('#doReset').on('click', function () {
                location.reload();
            });

        });

    </script>

</body>